package hotel.switchs.oversea.online.controller;

import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.dto.switchs.SwitchMasterHotelInfoDto;
import hotel.base.oversea.utils.Result;
import hotel.switchs.oversea.api.VO.SwitchMasterHotelVo;
import hotel.switchs.oversea.api.VO.request.GetMasterHotelListVo;
import hotel.switchs.oversea.api.VO.request.SwitchMasterHotelRedisParamsVo;
import hotel.switchs.oversea.online.config.service.RedisService;
import hotel.switchs.oversea.online.service.SwitchMasterHotelInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lmf
 * @Date 2020/5/14 14:38
 * 获取母酒店控制器
 **/
@RestController
@Slf4j
@RequestMapping("/switchHotelInfo")
@Api(value = "SwitchMasterHotelInfoController",tags = {"获取母酒店的相关信息"})
public class SwitchMasterHotelInfoController {
    @Autowired
    SwitchMasterHotelInfoService switchMasterHotelInfoService;

    @Autowired
    RedisService redisService;

    @Autowired
    RedisTemplate redisTemplate;

   /* @PostMapping("/getPageHotelIds")
    @ApiOperation(value = "获取酒店id",notes="必须经过auth授权")
    public Result<Object> queryPageHotelIds(@RequestBody JSONObject jsonObject){
        //参数校验
        int index = jsonObject.get("index")==null?-1:(int)jsonObject.get("index");
        int pageSize = jsonObject.get("pageSize")==null?-1:(int)jsonObject.get("pageSize");
        if(-1 == index){
            return new Result<>("index不可为空");
        }
        if(-1 == pageSize){
            return new Result<>("pageSize不可为空");
        }
        //发起请求并保存数据
        return  switchMasterHotelInfoService.queryPageIds(index,pageSize);
    }*/

    /**
     * 获取母酒店列表
     */
    @PostMapping("/getdltmasterhotellist")
    @ApiOperation(value = "获取母酒店列表", notes = "必须经过了OAuth授权")
    public Result<Object> getList(@RequestBody GetMasterHotelListVo getMasterHotelListVo){
        Map<String,Object> map=new HashMap<>();
        if(getMasterHotelListVo.getCityID()==0){
            return new Result<>("城市id不能为空");
        }
        map.put("cityID",getMasterHotelListVo.getCityID());
        if(getMasterHotelListVo.getMasterHotelID()!=0){
            map.put("masterHotelID",getMasterHotelListVo.getMasterHotelID());
        }
        if(!StringUtils.isEmpty(getMasterHotelListVo.getMasterHotelName())){
            map.put("masterHotelName",getMasterHotelListVo.getMasterHotelName());
        }
        return switchMasterHotelInfoService.getMasterHotelList(map,getMasterHotelListVo.getPageSize(),getMasterHotelListVo.getPageIndex());
    }

    @PostMapping("/saveMasterHotelInfos")
    @ApiOperation(value = "保存酒店信息接口",notes="必须经过auth授权")
    public Result<Object> saveMasterHotelInfos(@RequestBody SwitchMasterHotelRedisParamsVo switchMasterHotelRedisParamsVo){
        //发起请求并保存数据
        Map<String,List<SwitchMasterHotelVo>> dltMasterHotelEntityListMap = switchMasterHotelRedisParamsVo.getDltMasterHotelEntityListMap();
        if(null == dltMasterHotelEntityListMap || dltMasterHotelEntityListMap.size()==0){
            return new Result<>("参数dltMasterHotelEntityListMap不可为空", CommonConstants.REQUEST_ERROR);
        }
        return switchMasterHotelInfoService.saveMasterHotelInfo(dltMasterHotelEntityListMap);
    }

    @PostMapping("/resetSwitchsHotelDetailToRedis")
    @ApiOperation(value = "恢复重置携程母酒店数据到redis",notes="必须经过auth授权")
    public Result<Object> resetSwitchsHotelDetailToRedis(HttpServletRequest request){
        //删除数据
        redisTemplate.delete(CommonConstants.SWITCH_HOTEL_INFO);
        //重新拉取数据
        Map<String, SwitchMasterHotelInfoDto> putRedisHotelInfoMap = new HashMap<>();
        List<SwitchMasterHotelInfoDto> list = switchMasterHotelInfoService.queryPageReturnDto();
        for (SwitchMasterHotelInfoDto switchMasterHotelInfoDto:list){
            if(!(StringUtils.isEmpty(switchMasterHotelInfoDto.getHotelAddress()) && StringUtils.isEmpty(switchMasterHotelInfoDto.getBdLat())
                    && StringUtils.isEmpty(switchMasterHotelInfoDto.getGdLat()) && StringUtils.isEmpty(switchMasterHotelInfoDto.getGgLat()))){
                putRedisHotelInfoMap.put(String.valueOf(switchMasterHotelInfoDto.getMasterHotelId()),switchMasterHotelInfoDto);
            }
        }
        redisService.set(CommonConstants.SWITCH_HOTEL_INFO,putRedisHotelInfoMap);
        return new Result<>();
    }

    @PostMapping("/findSwitchsHotelDetailRedis")
    @ApiOperation(value = "查询携程酒店redis数据",notes="必须经过auth授权")
    public Result<Object> findSwitchsHotelDetailRedis(HttpServletRequest request){
        //redis中存储的酒店数据太大，本地跑不起来，放到测试服务器
        Map<String, SwitchMasterHotelInfoDto> redisMap = (Map<String,SwitchMasterHotelInfoDto>)redisService.get(CommonConstants.SWITCH_HOTEL_INFO);
        if(null == redisMap){
            redisMap = new HashMap<>();
        }
        return new Result<>(redisMap.size());
    }

}
